<?php


namespace Packages\User\Controllers\Client;


use App\Http\Controllers\Controller;
use Packages\User\Services\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    protected UserService $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    /**
     * 注册用户
     * @param Request $request
     * @return JsonResponse
     */
    public function register(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'nick_name' => 'required|string|min:1|max:32',
            'avatar' => 'required|string|max:255',
            'position' => 'required|string|max:32',
            // 'gender' => 'required|integer|between:0,2',
            // 'username' => 'required|string|min:3|max:32',
            'bind_phone' => 'required|regex:/^1[0-9]{10}$/',
            'password' => 'required|string|min:6|max:20',
            'code' => 'required|string|size:6',
        ]);
        $params['type'] = 1; // 普通用户
        $params['meta'] = [
            'position' => $params['position'],
        ]; // 普通用户

        // 短信code验证
        $smsCheck = app('smsService')->checkSMS($params['code'], $params['bind_phone']);
        if (!$smsCheck['status']) return $this->returnErrorResponse($smsCheck['msg']);

        $res = $this->userService->register($params);

        return $this->returnRes($res);
    }

    /**
     * 用户登录
     * @param Request $request
     * @return JsonResponse
     */
    public function login(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            // 'username' => 'required|string|min:3|max:32',
            'password' => 'string|min:6|max:20',
            // 'bind_email' => 'string|min:2|max:128',
            'bind_phone' => 'required|regex:/^1[0-9]{10}$/',
            'code' => 'string|size:6',
            'method' => 'required|string|max:16',
        ]);

        $params['last_ip'] = $request->getClientIp();

        // 手机号+验证码登录效验
        if ($params['method'] == 'bind_phone') {
            if (empty($params['code'])) return $this->returnErrorResponse('验证码不能为空!');

            $smsCheck = app('smsService')->checkSMS($params['code'], $params['bind_phone']);
            if (!$smsCheck['status']) return $this->returnErrorResponse($smsCheck['msg']);
        }

        $res = $this->userService->login($params);

        return $this->returnRes($res);
    }

    /**
     * 退出登录
     * @param Request $request
     * @return JsonResponse
     */
    public function loginOut(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            // 'guid' => 'required|string|max:32',
        ]);

        $res = $this->userService->loginOut($params['current_user']);

        return $this->returnRes($res);
    }

    /**
     * 更新用户信息
     * @param Request $request
     * @return JsonResponse
     */
    public function update(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'string|size:32',
            'nick_name' => 'string|min:2|max:32',
            'avatar' => 'string|max:128',
            'gender' => 'integer|between:0,2',
            'birthday' => 'integer',
        ]);

        $res = $this->userService->update($params['guid'], $params);

        return $this->returnResult($res, '更新失败!');
    }

    /**
     * 更新登录信息
     * @param Request $request
     * @return JsonResponse
     */
    public function loginUpdate(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|max:32',
            // 'username' => 'string|min:3|max:32',
            'old_password' => 'string|min:6|max:20',
            'password' => 'string|min:6|max:20',
            // 'bind_email' => 'string|min:2|max:128',
            'bind_phone' => 'regex:/^1[0-9]{10}$/',
            'code' => 'string|size:6'
        ]);

        // 修改手机号时需要效验验证码
        if (isset($params['bind_phone'])) {
            if (empty($params['code'])) return $this->returnErrorResponse('验证码不能为空!');

            $smsCheck = app('smsService')->checkSMS($params['code'], $params['bind_phone']);
            if (!$smsCheck['status']) return $this->returnErrorResponse($smsCheck['msg']);
        }

        $res = $this->userService->loginUpdate($params['guid'], $params);

        return $this->returnRes($res);
    }

    /**
     * 修改密码
     * @param Request $request
     * @return JsonResponse
     */
    public function updatePass(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|max:32',
            'old_password' => 'required|string|min:6|max:20',
            'password' => 'required|string|min:6|max:20',
        ]);

        $res = $this->userService->updatePass($params['guid'], $params);

        return $this->returnRes($res);
    }

    /**
     * 找回密码
     * @param Request $request
     * @return JsonResponse
     */
    public function findPassword(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'password' => 'required|string|min:6|max:32',
            'bind_phone' => 'required|regex:/^1[0-9]{10}$/',
            'code' => 'required|string|size:6'
        ]);

        // 验证码效验
        $smsCheck = app('smsService')->checkSMS($params['code'], $params['bind_phone']);
        if (!$smsCheck['status']) return $this->returnErrorResponse($smsCheck['msg']);

        $res = $this->userService->findPassword($params);

        return $this->returnRes($res);
    }

    /**
     * 用户详情
     * @param Request $request
     * @return JsonResponse
     */
    public function userInfo(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|max:32',
        ]);

        // 获取账户信息
        $result = $this->userService->userInfo($params['guid']);

        return $this->returnResult($result, '用户不存在!');
    }
}
